import config from './config.js'

var request = function (config) {
  if (config.loadingMessage == undefined) {
    wx.showLoading({
      title: '请稍侯...',
    })
  } else if (config.loadingMessage) {
    wx.showLoading({
      title: config.loadingMessage,
    })
  }
  wx.request({
    url: config.url,
    method: config.method,
    data: config.data,
    success: function (res) {
      if (config.loadingMessage || config.loadingMessage == undefined) {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        config.success(res.data)
      }
    },
    fail: function (err) {
      if (config.loadingMessage || config.loadingMessage == undefined) {
        wx.hideLoading()
      }
      config.fail(err)
    }
  })
}

/**
 * 注册接口
 * @param params {
 *     userPhone: 'string',
 *     password: 'string',
 *     invitecode: 'string',
 *     verificationCode: 'string',
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var register = function (params, success, fail, loadingMessage) {
  params.registerSys = config.platform
  request({
    url: config.registerUrl,
    method: "POST",
    data: params,
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 登录接口
 * @param params {
 *     userName: 'string',
 *     password: 'string'
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var login = function (params, success, fail, loadingMessage) {
  params.system = config.platform
  request({
    url: config.loginUrl,
    method: "POST",
    data: params,
    success: success,
    fail: fail,
    loadingMessage
  })
}


/**
 * 获取Banner接口
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getBanner = function (success, fail, loadingMessage) {
  request({
    url: config.getBannerListUrl,
    method: "GET",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取Banner下的3个商品广告入口的信息
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getThreeGoodsADInfo = function (success, fail, loadingMessage) {
  request({
    url: config.getThreeGoodsADInfoUrl,
    method: "GET",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取首页底部精品推荐
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getRecommendation = function (success, fail, loadingMessage) {
  request({
    url: config.getRecommendationUrl,
    method: "GET",
    success: success,
    fail: fail,
    loadingMessage
  })
}
/**
 * 获取一级分类
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getClassifyone = function (success, fail, loadingMessage) {
  request({
    url: config.getClassifyoneUrl,
    method: "GET",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取二级分类
 * @param classifyId 一级分类的Id
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getClassifyTwo = function (classifyId, success, fail, loadingMessage) {
  request({
    url: config.getClassifyTwoUrl,
    data: {
      classifyId
    },
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取三级分类
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getClassifyThree = function (classifyId, success, fail, loadingMessage) {
  request({
    url: config.getClassifyThreeUrl,
    data: {
      classifyId
    },
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取商品列表（包括3个商品广告入口、首页商品搜索）
 * @param params {
 *  keyword	string	搜索商品，若没有值为“”
    ClassifyThreeId	int	三级分类id，若从广告接口、首页商品搜索，默认值0，
    difference	int	默认0 ，分类，首页搜索
    1广告接口进入（新品推荐）
    2广告接口进入（热销单品）
    type	int	默认0 相关，1销量，2价格，3新品，
    page	int	页数默认1
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getGoodsList = function (params, success, fail, loadingMessage) {
  request({
    url: config.getGoodsListUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取商品详情
 * @param params {
    accessToken	string	登陆标识
    goodsCode	String	商品编号
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getGoodsDetails = function (params, success, fail, loadingMessage) {
  request({
    url: config.getGoodsDetailsUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取商品的规格
 * @param params {
    goodsCode	String	商品编号
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getGoodsSpecifications = function (params, success, fail, loadingMessage) {
  request({
    url: config.getGoodsSpecificationsUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 * 获取我的购物车
 * @param params {
    accessToken	string	登陆标识
    page	int	页码
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getMyCartInfo = function (params, success, fail, loadingMessage) {
  request({
    url: config.getMyCartInfoUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}
/**
 * 加入购物车
 * @param params {
   accessToken	string	登陆标识
    goodsCode	int	商品Code
    goodsNum	int	商品数量
    specificationsId	int	参数id
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var addGoodsToCart = function (params, success, fail, loadingMessage) {
  request({
    url: config.addGoodsToCartUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 *  删除购物车的商品
 * @param params {
    accessToken	string	登陆标识
    id	int	购物车id
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var deleteGoodsForCart = function (params, success, fail, loadingMessage) {
  request({
    url: config.deleteGoodsForCartUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 *  删除购物车的商品
 * @param params {
   accessToken	string	登陆标识
  editParam	jsonArray	编辑参数

 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var editGoodsForCart = function (params, success, fail, loadingMessage) {
  request({
    url: config.editGoodsForCartUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}


/**
 *  获取我的分享
 * @param params {
  accessToken	string	登陆标识
  page	int	页码
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var getMyShare = function (params, success, fail, loadingMessage) {
  request({
    url: config.getMyShareUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

/**
 *  获取个人详情
 * @param params {
  accessToken	string	用户登录唯一标识
 *   }
 * @param loadingMessage 请求加载显示的文本，默认显示“请稍侯...”，不显示传 ''(空字符串)
 */
var personalDetails = function (params, success, fail, loadingMessage) {
  request({
    url: config.personalDetailsUrl,
    data: params,
    method: "POST",
    success: success,
    fail: fail,
    loadingMessage
  })
}

module.exports = {
  login,
  register,
  getBanner,
  getThreeGoodsADInfo,
  getRecommendation,
  getClassifyone,
  getClassifyTwo,
  getClassifyThree,
  getGoodsList,
  getGoodsDetails,
  getGoodsSpecifications,
  getMyCartInfo,
  addGoodsToCart,
  deleteGoodsForCart,
  editGoodsForCart,
  getMyShare,
  personalDetails,
}